package com.huamao.zh.service.impl;

import static java.math.BigDecimal.ROUND_HALF_DOWN;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.huamao.zh.model.Account;
import com.huamao.zh.model.LogScore;
import com.huamao.zh.model.LogScoreFreeze;
import com.huamao.zh.model.SktAgents;
import com.huamao.zh.model.SktAgentsStockholder;
import com.huamao.zh.model.SktShops;
import com.huamao.zh.model.SktSysConfigs;
import com.huamao.zh.service.AccountService;
import com.huamao.zh.service.AgentsService;
import com.huamao.zh.service.AgentsStockholderService;
import com.huamao.zh.service.LogScoreFreezeService;
import com.huamao.zh.service.LogScoreService;
import com.huamao.zh.service.SalesMonthService;
import com.huamao.zh.service.ShareBonusService;
import com.huamao.zh.service.ShopsService;
import com.huamao.zh.service.SysConfigsService;

@Service
@Transactional
public class ShareBonusServiceImpl implements ShareBonusService {

	@Autowired
	private AgentsService agentsService;

	@Autowired
	private AgentsStockholderService agentsStockholderService;

	@Autowired
	private ShopsService shopsService;

	@Autowired
	private SalesMonthService salesMonthService;

	@Autowired
	private LogScoreFreezeService logScoreFreezeService;

	@Autowired
	private LogScoreService logScoreService;

	@Autowired
	private AccountService accountService;

	@Autowired
	private SysConfigsService sysConfigsService;

	public final static int NUM_HUNDRED = 100;
	public final static int NUM_SEVENTY = 70;
	public final static int NUM_TEN = 10;
	public final static int NUM_ELEVEN = 11;
	public final static int NUM_TWENTEEN = 12;
	public final static int NUM_FOURTEEN = 14;

	public final static int UNBAT = 1;
	public final static int BATING = 9;
	public final static int BATED = 2;

	/**
	 * 股东分红流程入口方法
	 *
	 * @throws Exception
	 */
	@Override
	public void shareBonus() throws Exception {

		// LogUtil.info(LogCodeType.CODE_7003, "======enter===股东分红======");
		synchronized (this.getClass()) {
			// 先做判断，这个月要不要分红
			SktSysConfigs lastDividendMonth = sysConfigsService.getByFieldCode("lastDividendMonth");// 上次分红月份
			String lastDividendMonthStr = lastDividendMonth.getFieldValue();
			String dividendPeriodStr = sysConfigsService.getByFieldCode("dividendPeriod").getFieldValue();// 分红周期

			int monthNum = Math.addExact(Integer.valueOf(lastDividendMonthStr), Integer.valueOf(dividendPeriodStr));
			if (monthNum > NUM_TWENTEEN) {
				monthNum -= NUM_TWENTEEN;
			}
			int currentMonth = Calendar.getInstance().get(Calendar.MONTH) + 1;
			// LogUtil.info(LogCodeType.CODE_7003,
			// "monthNum ：" + monthNum + " ================ currentMonth : " +
			// currentMonth);

			if (monthNum != currentMonth) {
				// LogUtil.info(LogCodeType.CODE_7003,
				// "当前月份不是分红月份============");
				return;
			}

			Map<String, Object> agentsMap = new HashMap<String, Object>();
			// agentsMap.put("agentStatus", 1);
			int listCount = agentsService.getListCount(UNBAT);
			// LogUtil.info(LogCodeType.CODE_7003, "======count======" +
			// listCount);

			List<SktAgents> agents = agentsService.queryList(agentsMap);

			for (SktAgents agent : agents) {

				int stockNum = agent.getStockNum();
				int storeNum = agent.getStoreNum();
				int totalNum = agent.getTotalNum();
				int totalStoreNum = agent.getTotalStoreNum();

				if (totalNum == 0 || stockNum == 0) {
					return;
				}

				int stockRatio = stockNum * NUM_HUNDRED / totalNum;
				int storeRatio = storeNum * NUM_HUNDRED / totalStoreNum;

				// LogUtil.info(LogCodeType.CODE_7003,
				// "======stockRatio======" + stockRatio + "=====storeRatio===="
				// + storeRatio);

				if (stockRatio == NUM_HUNDRED && storeRatio >= NUM_HUNDRED) {
					// LogUtil.info(LogCodeType.CODE_7003,
					// "====两个完成度都是百分之百=====");
					dealEntire(agent, lastDividendMonth.getFieldValue(), dividendPeriodStr);

				}
				if (((stockRatio > NUM_SEVENTY && stockRatio <= NUM_HUNDRED)
						&& (storeRatio > NUM_SEVENTY && storeRatio < NUM_HUNDRED))
						|| ((stockRatio > NUM_SEVENTY && stockRatio < NUM_HUNDRED)
								&& (storeRatio > NUM_SEVENTY && storeRatio <= NUM_HUNDRED))) {

					// LogUtil.info(LogCodeType.CODE_7003,
					// "====完成度都在百分之七十到百分之一百之间=====");
					dealMore(agent, lastDividendMonth.getFieldValue(), dividendPeriodStr);

				}

				if (stockRatio < NUM_SEVENTY || storeRatio < NUM_SEVENTY) {
					// LogUtil.info(LogCodeType.CODE_7003,
					// "====完成度都小于百分之七十=====");
					dealLess(agent, lastDividendMonth.getFieldValue(), dividendPeriodStr);
				}

			}
			// 完成后更新batFlag状态
			/*
			 * agentsMap.clear(); agentsMap.put("batTarget", BATED);
			 * agentsMap.put("batFrom", BATING);
			 */
			/* agentsService.updateDealFlag(agentsMap); */
			// 更新分红月份
			lastDividendMonth.setFieldValue(currentMonth + "");
			sysConfigsService.update(lastDividendMonth);

		}
	}

	/**
	 * 完成度百分之百处理流程
	 *
	 * @param agent
	 * @param lastDividendMonth
	 * @param dividendPeriod
	 * @throws Exception
	 */
	private void dealEntire(SktAgents agent, String lastDividendMonth, String dividendPeriod) throws Exception {

		int agentId = agent.getAgentId();
		// LogUtil.info(LogCodeType.CODE_7003, "====agentId: " + agentId);

		List<SktAgentsStockholder> stockHolderList = agentsStockholderService.getByAgentId(agentId);
		for (SktAgentsStockholder stockHolder : stockHolderList) {
			// 全部完成的分红系数
			BigDecimal threeRatio = agent.getThreeRatio();

			Map<String, Object> map = new HashMap<String, Object>();

			int level = agent.getAlevel();
			// level为代理级别，10省，11市，12区县
			// LogUtil.info(LogCodeType.CODE_7003, "====level: " + level);
			// 省
			if (level == NUM_TEN) {

				int provinceId = agent.getProvinceId();
				map.put("provinceId", provinceId);
				dealEntireMain(threeRatio, map, agent, stockHolder, lastDividendMonth, dividendPeriod);
			}

			// 市
			if (level == NUM_ELEVEN) {

				int cityId = agent.getCityId();
				map.put("cityId", cityId);
				dealEntireMain(threeRatio, map, agent, stockHolder, lastDividendMonth, dividendPeriod);
			}

			// 区县
			if (level == NUM_TWENTEEN) {

				int areaId = agent.getAreaId();
				map.put("areaId", areaId);
				dealEntireMain(threeRatio, map, agent, stockHolder, lastDividendMonth, dividendPeriod);
			}

		}

	}

	private void dealEntireMain(BigDecimal threeRatio, Map<String, Object> map, SktAgents agent,
			SktAgentsStockholder stockHolder, String lastDividendMonth, String dividendPeriod) throws Exception {

		// LogUtil.info(LogCodeType.CODE_7003, "dealEntireMain
		// ====================== ");

		List<SktShops> shopList = shopsService.getShopByLevelId(map);
		String dividendRatio = sysConfigsService.getByFieldCode("DividendRatio").getFieldValue();

		for (SktShops shop : shopList) {
			if (shop == null) {
				// LogUtil.info(LogCodeType.CODE_7003, "====找不到商店: ");
				return;
			}

			// 需要查询当月总销售额，根据分红周期计算==========================================================================
			// 查询月销售额
			Map<String, Object> sMap = new HashMap();
			// 算出上次分红的时间
			SimpleDateFormat format = new SimpleDateFormat("yyyyMM");
			Calendar cale = Calendar.getInstance();
			cale.setTime(new Date());
			Date c = cale.getTime();
			String nowTime = format.format(c);
			cale.add(Calendar.MONTH, -Integer.valueOf(dividendPeriod));
			Date m = cale.getTime();
			String LastDividentTime = format.format(m);

			sMap.put("nowTime", nowTime);
			sMap.put("LastDividentTime", LastDividentTime);
			sMap.put("shopId", shop.getShopId());
			// LogUtil.info(LogCodeType.CODE_7003, "上次分红日期=======" +
			// LastDividentTime + " 当前日期======" + nowTime);

			BigDecimal totalTurnover = salesMonthService.getSalesMonth(sMap);
			if (totalTurnover == null) {
				// LogUtil.info(LogCodeType.CODE_7003, "====找不到月销售额: ");
				return;
			}

			// 分红百分比

			BigDecimal rate = new BigDecimal(Integer.valueOf(dividendRatio) / NUM_HUNDRED);

			// 最终金额
			totalTurnover = totalTurnover.multiply(rate);
			// 总股份数
			int totalStock = agent.getTotalStockNum();
			// 股东持有份数
			int stock = stockHolder.getStockNum();

			// 完成100%总积分
			BigDecimal totalScoreThree = threeRatio.multiply(totalTurnover)
					.divide(new BigDecimal(totalStock), 10, ROUND_HALF_DOWN).multiply(new BigDecimal(stock));

			Account account = accountService.getByUserId(stockHolder.getUserId());
			BigDecimal preScore = account.getTotalScore();
			BigDecimal freezeScore = account.getFreezeScore();

			// 获取股东分红人数
			SktSysConfigs config = sysConfigsService.getByFieldCode("holderDividendNum");
			int holderDividendNum = Integer.valueOf(config.getFieldValue());
			int storeNum = stockHolder.getStoreNum();

			// 完成直推商家任务
			if (storeNum >= holderDividendNum) {
				// LogUtil.info(LogCodeType.CODE_7003, "记录流水日志
				// ====================== ");
				account.setScore(totalScoreThree);
				account.setTotalScore(totalScoreThree);
				// 股东日志
				LogScore logScore = new LogScore();
				logScore.setType(NUM_ELEVEN);
				logScore.setScore(totalScoreThree);
				logScore.setRemark("股东分红积分增加");
				addScoreLog(logScore, account, preScore);
			}

			// 未完成直推商家任务
			if (storeNum < holderDividendNum) {
				// LogUtil.info(LogCodeType.CODE_7003, "记录流水日志
				// ====================== ");
				account.setFreezeScore(totalScoreThree);
				// 记日志
				LogScoreFreeze logScoreFreezeLess = new LogScoreFreeze();
				logScoreFreezeLess.setType(15);
				logScoreFreezeLess.setScore(totalScoreThree);
				logScoreFreezeLess.setRemark("股东分红冻结积分增加");
				addLogScoreFreeze(logScoreFreezeLess, account, freezeScore);
			}

			// 取都是score，更新正式更新totalscore和score，冻结更新freezeScore
			accountService.updateBonusScore(account);

			// 更新股东分红表
			stockHolder.setLastDividend(totalScoreThree);
			stockHolder.setDividendTime(new Date());
			stockHolder.setTotalDividend(totalScoreThree.add(stockHolder.getTotalDividend()));

			agentsStockholderService.update(stockHolder);
		}
	}

	/**
	 * 两个完成度都在百分之七十到百分之一百之间
	 *
	 * @param agent
	 * @param lastDividendMonth
	 * @param dividendPeriod
	 * @throws Exception
	 */
	private void dealMore(SktAgents agent, String lastDividendMonth, String dividendPeriod) throws Exception {

		int agentId = agent.getAgentId();
		// LogUtil.info(LogCodeType.CODE_7003, "====agentId: " + agentId);

		List<SktAgentsStockholder> stockHolderList = agentsStockholderService.getByAgentId(agentId);

		for (SktAgentsStockholder stockHolder : stockHolderList) {
			// 完成70%(落地)的分红系数
			BigDecimal twoRatio = agent.getTwoRatio();
			// 全部完成的分红系数
			BigDecimal threeRatio = agent.getThreeRatio();

			Map<String, Object> map = new HashMap<String, Object>();

			int level = agent.getAlevel();
			// level为代理级别，10省，11市，12区县
			// LogUtil.info(LogCodeType.CODE_7003, "====level: " + level);
			// 省
			if (level == NUM_TEN) {

				int provinceId = agent.getProvinceId();
				map.put("provinceId", provinceId);
				dealMoreMain(twoRatio, threeRatio, map, agent, stockHolder, lastDividendMonth, dividendPeriod);
			}

			// 市
			if (level == NUM_ELEVEN) {

				int cityId = agent.getCityId();
				map.put("cityId", cityId);
				dealMoreMain(twoRatio, threeRatio, map, agent, stockHolder, lastDividendMonth, dividendPeriod);
			}

			// 区县
			if (level == NUM_TWENTEEN) {

				int areaId = agent.getAreaId();
				map.put("areaId", areaId);
				dealMoreMain(twoRatio, threeRatio, map, agent, stockHolder, lastDividendMonth, dividendPeriod);
			}
		}

	}

	private void dealMoreMain(BigDecimal twoRatio, BigDecimal threeRatio, Map<String, Object> map, SktAgents agent,
			SktAgentsStockholder stockHolder, String lastDividendMonth, String dividendPeriod) throws Exception {

		// LogUtil.info(LogCodeType.CODE_7003, "dealMoreMain
		// ====================== ");

		List<SktShops> shopList = shopsService.getShopByLevelId(map);
		for (SktShops shop : shopList) {
			if (shop == null) {
				// LogUtil.info(LogCodeType.CODE_7003, "====找不到商店: ");
				return;
			}

			// 查询月销售额
			Map<String, Object> sMap = new HashMap();
			// 算出上次分红的时间
			SimpleDateFormat format = new SimpleDateFormat("yyyyMM");
			Calendar cale = Calendar.getInstance();
			cale.setTime(new Date());
			Date c = cale.getTime();
			String nowTime = format.format(c);
			cale.add(Calendar.MONTH, -Integer.valueOf(dividendPeriod));
			Date m = cale.getTime();
			String LastDividentTime = format.format(m);

			sMap.put("nowTime", nowTime);
			sMap.put("LastDividentTime", LastDividentTime);
			sMap.put("shopId", shop.getShopId());
			// LogUtil.info(LogCodeType.CODE_7003, "上次分红日期=======" +
			// LastDividentTime + " 当前日期======" + nowTime);

			BigDecimal totalTurnover = salesMonthService.getSalesMonth(sMap);
			if (totalTurnover == null) {
				// LogUtil.info(LogCodeType.CODE_7003, "====找不到月销售额: ");
				return;
			}

			// 分红百分比
			String dividendRatio = sysConfigsService.getByFieldCode("DividendRatio").getFieldValue();
			BigDecimal rate = new BigDecimal(Integer.valueOf(dividendRatio) / NUM_HUNDRED);

			// 最终金额
			totalTurnover = totalTurnover.multiply(rate);
			// 总股份数
			int totalStock = agent.getTotalStockNum();
			// 股东持有份数
			int stock = stockHolder.getStockNum();

			// 完成70%总积分
			BigDecimal totalScoreTwo = twoRatio.multiply(totalTurnover)
					.divide(new BigDecimal(totalStock), 10, ROUND_HALF_DOWN).multiply(new BigDecimal(stock));
			// 完成100%总积分
			BigDecimal totalScoreThree = threeRatio.subtract(twoRatio).multiply(totalTurnover)
					.divide(new BigDecimal(totalStock), 10, ROUND_HALF_DOWN).multiply(new BigDecimal(stock));

			Account account = accountService.getByUserId(stockHolder.getUserId());
			BigDecimal preScore = account.getTotalScore();
			BigDecimal freezeScore = account.getFreezeScore();

			// 获取股东分红人数
			SktSysConfigs config = sysConfigsService.getByFieldCode("holderDividendNum");
			int holderDividendNum = Integer.valueOf(config.getFieldValue());
			int storeNum = stockHolder.getStoreNum();

			// LogUtil.info(LogCodeType.CODE_7003, "记录流水日志
			// ====================== ");

			// 完成100%的日志
			LogScoreFreeze logScoreFreezeAll = new LogScoreFreeze();
			account.setFreezeScore(totalScoreThree);
			logScoreFreezeAll.setType(16);
			logScoreFreezeAll.setScore(totalScoreThree);
			logScoreFreezeAll.setRemark("股东分红冻结积分增加");
			addLogScoreFreeze(logScoreFreezeAll, account, freezeScore);

			// 完成直推商家任务
			if (storeNum >= holderDividendNum) {
				account.setScore(totalScoreTwo);
				account.setTotalScore(totalScoreTwo);
				// 股东日志
				LogScore logScore = new LogScore();
				logScore.setType(11);
				logScore.setScore(totalScoreTwo);
				logScore.setRemark("股东分红积分增加");
				addScoreLog(logScore, account, preScore);

			}

			// 完成70%，未完成直推商家任务
			if (storeNum < holderDividendNum) {
				account.setFreezeScore(totalScoreThree.add(totalScoreTwo));
				// 记日志
				LogScoreFreeze logScoreFreezeLess = new LogScoreFreeze();
				logScoreFreezeLess.setType(15);
				logScoreFreezeLess.setScore(totalScoreTwo);
				logScoreFreezeLess.setRemark("股东分红冻结积分增加");
				addLogScoreFreeze(logScoreFreezeLess, account, freezeScore.add(totalScoreTwo));
			}

			// 取都是score，更新正式更新totalscore和score，冻结更新freezeScore
			accountService.updateBonusScore(account);

			// 更新股东分红表
			stockHolder.setLastDividend(totalScoreThree.add(totalScoreTwo));
			stockHolder.setDividendTime(new Date());
			stockHolder.setTotalDividend(totalScoreThree.add(totalScoreTwo).add(stockHolder.getTotalDividend()));

			agentsStockholderService.update(stockHolder);
		}

	}

	/**
	 * 两个完成度都小于百分之七十
	 *
	 * @param agent
	 * @param lastDividendMonth
	 * @param dividendPeriod
	 * @throws Exception
	 */
	private void dealLess(SktAgents agent, String lastDividendMonth, String dividendPeriod) throws Exception {

		int agentId = agent.getAgentId();
		// LogUtil.info(LogCodeType.CODE_7003, "====agentId: " + agentId);

		// 查找股东集合
		List<SktAgentsStockholder> stockHolderList = agentsStockholderService.getByAgentId(agentId);

		// 遍历股东集合
		for (SktAgentsStockholder stockHolder : stockHolderList) {
			// 成为股东的分红系数
			BigDecimal oneRatio = agent.getOneRatio();
			// 完成70%(落地)的分红系数
			BigDecimal twoRatio = agent.getTwoRatio();
			// 全部完成的分红系数
			BigDecimal threeRatio = agent.getThreeRatio();

			Map<String, Object> map = new HashMap<String, Object>();

			int level = agent.getAlevel();
			// level为代理级别，10省，11市，12区县
			// LogUtil.info(LogCodeType.CODE_7003, "====level: " + level);
			// 省
			if (level == NUM_TEN) {
				// LogUtil.info(LogCodeType.CODE_7003, "level：10 ==============
				// ");
				int provinceId = agent.getProvinceId();
				map.put("provinceId", provinceId);
				dealLessMain(oneRatio, twoRatio, threeRatio, map, agent, stockHolder, lastDividendMonth,
						dividendPeriod);
			}

			// 市
			if (level == NUM_ELEVEN) {
				// LogUtil.info(LogCodeType.CODE_7003, "level：11 ==============
				// ");
				int cityId = agent.getCityId();
				map.put("cityId", cityId);
				dealLessMain(oneRatio, twoRatio, threeRatio, map, agent, stockHolder, lastDividendMonth,
						dividendPeriod);
			}

			// 区县
			if (level == NUM_TWENTEEN) {
				// LogUtil.info(LogCodeType.CODE_7003, "level：12 ==============
				// ");
				int areaId = agent.getAreaId();
				map.put("areaId", areaId);
				dealLessMain(oneRatio, twoRatio, threeRatio, map, agent, stockHolder, lastDividendMonth,
						dividendPeriod);
			}
		}

	}

	private void dealLessMain(BigDecimal oneRatio, BigDecimal twoRatio, BigDecimal threeRatio, Map<String, Object> map,
			SktAgents agent, SktAgentsStockholder stockHolder, String lastDividendMonth, String dividendPeriod)
			throws Exception {

		// LogUtil.info(LogCodeType.CODE_7003, "dealLessMain
		// ======================");

		List<SktShops> shopList = shopsService.getShopByLevelId(map);
		if (shopList.size() == 0) {
			// LogUtil.info(LogCodeType.CODE_7003, "====找不到商店: ");
			return;
		}

		for (SktShops shop : shopList) {

			// 查询月销售额
			Map<String, Object> sMap = new HashMap();
			// 算出上次分红的时间
			SimpleDateFormat format = new SimpleDateFormat("yyyyMM");
			Calendar cale = Calendar.getInstance();
			cale.setTime(new Date());
			Date c = cale.getTime();
			String nowTime = format.format(c);
			cale.add(Calendar.MONTH, -Integer.valueOf(dividendPeriod));
			Date m = cale.getTime();
			String LastDividentTime = format.format(m);

			sMap.put("nowTime", nowTime);
			sMap.put("LastDividentTime", LastDividentTime);
			sMap.put("shopId", shop.getShopId());
			// LogUtil.info(LogCodeType.CODE_7003, "上次分红日期=======" +
			// LastDividentTime + " 当前日期======" + nowTime);

			BigDecimal totalTurnover = salesMonthService.getSalesMonth(sMap);
			if (totalTurnover == null) {
				// LogUtil.info(LogCodeType.CODE_7003, "====找不到月销售额: ");
				return;
			}

			// 分红百分比
			String dividendRatio = sysConfigsService.getByFieldCode("DividendRatio").getFieldValue();
			BigDecimal rate = new BigDecimal(Integer.valueOf(dividendRatio) / NUM_HUNDRED);
			// 最终金额
			totalTurnover = totalTurnover.multiply(rate);
			// 总股份数
			int totalStock = agent.getTotalStockNum();
			// 股东持有份数
			int stock = stockHolder.getStockNum();

			// 股东总积分
			BigDecimal totalScoreOne = oneRatio.multiply(totalTurnover)
					.divide(new BigDecimal(totalStock), 10, ROUND_HALF_DOWN).multiply(new BigDecimal(stock));
			// 完成70%总积分
			BigDecimal totalScoreTwo = twoRatio.subtract(oneRatio).multiply(totalTurnover)
					.divide(new BigDecimal(totalStock), 10, ROUND_HALF_DOWN).multiply(new BigDecimal(stock));
			// 完成100%总积分
			BigDecimal totalScoreThree = threeRatio.subtract(oneRatio).multiply(totalTurnover)
					.divide(new BigDecimal(totalStock), 10, ROUND_HALF_DOWN).multiply(new BigDecimal(stock));

			Account account = accountService.getByUserId(stockHolder.getUserId());
			BigDecimal freezeScore = account.getFreezeScore();

			// 取都是score，更新正式更新totalscore和score，冻结更新freezeScore
			BigDecimal totalScore = account.getTotalScore();
			account.setScore(totalScoreOne);
			account.setTotalScore(totalScoreOne);
			account.setFreezeScore(totalScoreTwo.add(totalScoreThree));
			accountService.updateBonusScore(account);

			// LogUtil.info(LogCodeType.CODE_7003,
			// "记录流水日志=========================");
			// 股东日志
			LogScore logScore = new LogScore();
			logScore.setType(NUM_ELEVEN);
			logScore.setScore(totalScoreOne);
			logScore.setRemark("股东分红积分增加");
			addScoreLog(logScore, account, totalScore);

			// 完成70%的日志
			LogScoreFreeze logScoreFreezeLess = new LogScoreFreeze();
			logScoreFreezeLess.setType(15);
			logScoreFreezeLess.setScore(totalScoreTwo);
			logScoreFreezeLess.setRemark("股东分红冻结积分增加");
			addLogScoreFreeze(logScoreFreezeLess, account, freezeScore);

			// 完成100%的日志
			LogScoreFreeze logScoreFreezeAll = new LogScoreFreeze();
			logScoreFreezeAll.setType(16);
			logScoreFreezeAll.setScore(totalScoreThree);
			logScoreFreezeAll.setRemark("股东分红冻结积分增加");
			BigDecimal preScore = freezeScore.add(totalScoreTwo);
			addLogScoreFreeze(logScoreFreezeAll, account, freezeScore.add(totalScoreTwo));

			// 更新股东分红表
			stockHolder.setLastDividend(totalScoreThree.add(totalScoreTwo).add(totalScoreOne));
			stockHolder.setDividendTime(new Date());
			stockHolder.setTotalDividend(
					totalScoreThree.add(totalScoreTwo).add(totalScoreOne).add(stockHolder.getTotalDividend()));

			agentsStockholderService.update(stockHolder);
		}

	}

	private void addLogScoreFreeze(LogScoreFreeze logScoreFreeze, Account account, BigDecimal preScore)
			throws Exception {

		logScoreFreeze.setFromId(0);
		logScoreFreeze.setUserId(account.getUserId());
		logScoreFreeze.setOrderNo("");// 订单号为空
		logScoreFreeze.setPreScore(preScore);
		logScoreFreeze.setScoreType(1);
		logScoreFreeze.setDataFlag(1);
		logScoreFreeze.setCreateTime(new Date());
		logScoreFreezeService.add(logScoreFreeze);

	}

	private void addScoreLog(LogScore logScore, Account account, BigDecimal preScore) throws Exception {

		logScore.setFromId(0);
		logScore.setUserId(account.getUserId());
		logScore.setOrderNo("");// 订单号为空
		logScore.setPreScore(preScore);
		logScore.setScoreType(1);
		logScore.setDataFlag(1);
		logScore.setCreateTime(new Date());
		logScoreService.add(logScore);

	}

	@Override
	public SktSysConfigs selectOne(SktSysConfigs sktSysConfigs) {
		SktSysConfigs sysConfigs = sysConfigsService.selectOne(sktSysConfigs);
		return sysConfigs;
	}

	@Override
	public List<SktSysConfigs> selectAll() {
		return sysConfigsService.selectAll();
	}

}
